package com.mj.renminbi.util;

import java.util.HashMap;
import java.util.Map;

/**
 * 人民币大写转换
 * @author zhaominglei
 * @date 2015-3-4
 * 
 */
public class RMBconversion {
    public static final double MAXIMUM_NUMBER = 99999999999.99d; 
    public String CN_ZERO = "零"; 
    public String CN_ONE = "壹"; 
    public String CN_TWO = "贰"; 
    public String CN_THREE = "叁"; 
    public String CN_FOUR = "肆"; 
    public String CN_FIVE = "伍"; 
    public String CN_SIX = "陆"; 
    public String CN_SEVEN = "柒"; 
    public String CN_EIGHT = "捌"; 
    public String CN_NINE = "玖"; 
    public String CN_TEN = "拾"; 
    public String CN_HUNDRED = "佰"; 
    public String CN_THOUSAND = "仟"; 
    public String CN_TEN_THOUSAND = "万"; 
    public String CN_HUNDRED_MILLION = "亿"; 
    public String CN_SYMBOL = "人民币"; 
    public String CN_DOLLAR = "元"; 
    public String CN_TEN_CENT = "角"; 
    public String CN_CENT = "分"; 
    public String CN_INTEGER = "整";
    @SuppressWarnings("serial")
    public Map<String, String> map = new HashMap<String, String>(){
    	{ 
    		put("0", "零");
    		put("1", "壹");
    		put("2", "贰");
    		put("3", "叁");
    		put("4", "肆");
    		put("5", "伍");
    		put("6", "陆");
    		put("7", "柒");
    		put("8", "捌");
    		put("9", "玖");
    	}
    };
	
	@SuppressWarnings("unused")
	public static void main(String[] args) {
		RMBconversion rmbconversion = new RMBconversion();
//		System.out.println(rmbconversion.conversion1("10101001010"));
//		System.out.println(rmbconversion.conversion1("10110110.22"));
//		System.out.println(rmbconversion.conversion2("10110110.HE2"));
//		System.out.println(rmbconversion.conversion2("10101001010"));
	}
	
    // 自己写的有待提高
	@SuppressWarnings("unused")
	public String conversion1(String rmb) {
		if (rmb == null || rmb.equals("")) {
			return null;
		}
		StringBuilder result = new StringBuilder(CN_SYMBOL);
		if(Double.parseDouble(rmb) > MAXIMUM_NUMBER) {
			return null;
		}
		
		String integer,decimal;
		String[] ss = rmb.split("\\.");
		if (ss.length == 2) {
			integer = ss[0];
			decimal = ss[1];
		} else {
			integer = ss[0];
			decimal = null;
		}
		boolean isInt = false;
		try {
			long integer2 = Long.parseLong(integer);
			isInt = true;
		} catch (Exception e) {
			isInt = false;
		}
		
		int zeroCount = 0;
		if (isInt) {
			for (int i = 0; i < integer.length(); i++) {
				int p = integer.length()-i;
				int mount = p%4;
				String str = String.valueOf(integer.charAt(i));
				if (!str.equals("0")) {
					result.append(map.get(str));
					if (mount == 0) {
						result.append(CN_THOUSAND);
					}
					if (mount == 3) {
						result.append(CN_HUNDRED);
					}
					if (mount == 2) {
						result.append(CN_TEN);
					}
					if (mount == 1) {
						if (p == 1) {
							result.append(CN_DOLLAR);
						}
						if (p == 5) {
							result.append(CN_TEN_THOUSAND);
						}
						if (p == 9) {
							result.append(CN_HUNDRED_MILLION);
						}
					}
				} else {
					zeroCount++;
					if (p == 1) {
						result.append(CN_DOLLAR);
					} else {
						result.append(map.get(str));
					}
					if (mount == 1) {
						if (p == 5) {
							result.append(CN_TEN_THOUSAND);
						}
						if (p == 9) {
							result.append(CN_HUNDRED_MILLION);
						}
					}
				}
			}
			
			String s = result.toString();
			if(zeroCount == integer.length()-1) {
				s = s.replaceAll("零", "");
				result = new StringBuilder(s);
			} else {
				s = s.replaceAll("零零", "").replaceAll("零零零", "").replaceAll("零元", "元");
				result = new StringBuilder(s);
			}
		}
		
		if (decimal == null || decimal.equals("")) {
			result.append(CN_INTEGER);
		} else {
			if (decimal.length() > 1) {
				result.append(map.get(String.valueOf(decimal.charAt(0)))).append(CN_TEN_CENT);
				result.append(map.get(String.valueOf(decimal.charAt(1)))).append(CN_CENT);
			} else {
				result.append(map.get(decimal)).append(CN_TEN_CENT);
			}
		}
		
		return result.toString();
	}
	
	// 参考网上的 学习
	public String conversion2(String rmb) {
		if (rmb == "" || rmb.equals("")) {
			return null;
		}
		String currencyDigits = rmb.replaceAll("\\.", "");
		for (int i = 0; i < currencyDigits.length(); i++) {
			if (!Character.isDigit(currencyDigits.charAt(i))) {
				return null;
			}
		}
		if(Double.parseDouble(rmb) > MAXIMUM_NUMBER) {
			return null;
		}
		String integral,decimal;
		String[] parts = rmb.split("\\.");
		if (parts.length > 1) {
			integral = parts[0];
			decimal = parts[1];
			
			if (decimal.length() > 1) {
				decimal = decimal.substring(0, 2);
			}
		} else {
			integral = parts[0];
			decimal = "";
		}
		String[] digits = new String[]{CN_ZERO, CN_ONE, CN_TWO, CN_THREE, CN_FOUR, CN_FIVE, CN_SIX, CN_SEVEN, CN_EIGHT, CN_NINE};
		String[] radices = new String[]{"", CN_TEN, CN_HUNDRED, CN_THOUSAND};
		String[] bigRadices = new String[]{"", CN_TEN_THOUSAND, CN_HUNDRED_MILLION};
		String[] decimals = new String[]{CN_TEN_CENT, CN_CENT};
		StringBuilder result = new StringBuilder(CN_SYMBOL);
		try {
			String d;
			if (Long.parseLong(integral) > 0) {
				int zeroCount = 0; 
				for (int i = 0; i < integral.length(); i++) {
					int p = integral.length()-i-1;
					d = String.valueOf(integral.charAt(i));
					int quotient = p / 4;
					int modulus = p % 4;
					if (d.equals("0")) {
						zeroCount++;
					} else {
						if (zeroCount > 0) {
							result.append(digits[0]);
						}
						zeroCount = 0;
						result.append(digits[Integer.valueOf(d)]+radices[modulus]);
					}
					if (modulus == 0 && zeroCount < 4) {
						result.append(bigRadices[quotient]);
						zeroCount = 0;
					}
				}
				result.append(CN_DOLLAR);
			}
			if (decimal != null && !decimal.equals("")) {
				for (int i = 0; i < decimal.length(); i++) {
					d = String.valueOf(decimal.charAt(i));
					if (!d.equals("0")) {
						result.append(digits[Integer.valueOf(d)]+decimals[i]);
					}
				}
			}
			if (result.toString().equals("")) {
				result.append(CN_ZERO + CN_DOLLAR);
			}
			if (decimal == null || decimal.equals("")) {
				result.append(CN_INTEGER);
			}
		} catch (NumberFormatException e) {
			e.printStackTrace();
		}
		return result.toString();
	}
}
